<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2025/7/8
 * Time: 11:11
 */

namespace app\controller\sys;


use app\controller\Base;
use app\model\Image AS model;
use think\facade\Db;

class Image extends Base
{
    public function list(){
        $id = input('id',0);
        $list = model::orderRaw('sorts DESC,total_count DESC,create_time DESC')
            ->paginate(['page' => $this->page,'list_rows' => $this->size]);
        return $this->msg($list);
    }

    public function top($id){
        $find = model::where('id',$id)->find();
        if ($find->sorts > 0) $find->sorts = 0;
        else $find->sorts = model::max('sorts') + 1;
        $find->save();
        return $this->msg();
    }

    public function save(){
        try {
            Db::startTrans();
            $id = input('id');
            $post = input('post.');
            if (empty($id)) {
                $find = model::create($post);
            } else {
                $find = model::find($id);
                $find->save($post);
            }
            Db::commit();
            return $this->msg($find);
        }catch (\Exception $e){
            Db::rollback();
            return $this->fail($e->getMessage());
        }
    }

    public function delete(){
        $id = input('id');
        $find = model::find($id);
        if (empty($find->id))
            return $this->fail('数据不存在');
        unlink('.'.$find->src);
        $find->delete();
        return $this->msg();
    }
}